<?php

class CBitAuctionBtnFeature extends CWidget
{

    public $title = 'BID';

    public $class = 'btn btn-bid-auction';

    public $auctionId;

    public $bidAjaxUrl = null;

    public $htmlOptions = array();

    public function init()
    {
        parent::init();
        if ($this->bidAjaxUrl === null) {
            $this->bidAjaxUrl = Yii::app()->baseUrl . '/ajax/ajaxBidAuction/';
        }
        $this->htmlOptions['class'] = $this->class . ' event-bid-auction';
        $this->htmlOptions['data-auctionId'] = $this->auctionId;
        $this->htmlOptions['href'] = "http://pa.egunner.com/login";
        $this->htmlOptions['target'] = "_blank";
        $this->renderBtn();
    }

    public function renderBtn()
    {
        echo CHtml::linkButton($this->title, $this->htmlOptions);
        $this->buildJs();
    }

    protected function buildJs()
    {
        Yii::app()->clientScript->registerScriptFile(Yii::app()->resource->path('js') . "/client.js");
        $rUrl = str_replace(Yii::app()->request->baseUrl . '/', null, Yii::app()->request->getUrl());
        $loginUrl ='http://pa.egunner.com/login?url=' . $rUrl;
        Yii::app()->clientScript->registerScript('bidAuctionBtn', "
            client.bidAuction.init({
        		'url':'{$this->bidAjaxUrl}',
        		'event':{
        			'bidAjax.success':function(response){
        			    console.log(response);
        			    if(response.hasOwnProperty('isGuest')){
        					client.setHash('needLogin',1);return false;
        				}
        				if(response.hasOwnProperty('overcost')){
        					var overcost=client.getHash('overcost');
        					if(!overcost){
                                overcost=[];
                            }
                            if(!(response.overcost in overcost)){
                                overcost.push(response.overcost);
                            }
                            console.log(overcost);
                            client.setHash('overcost',overcost);
        				}
        				if(response.hasOwnProperty('error')){
        					if(!response.error.endsWith('Cannot place bid. You are the highest bidder.')){
            					alert(response.error);
                            }else{
                                client.setHash('winner_error',response.error);
                                setTimeout(function(){
									client.delHash('winner_error');
								},5000);
                                console.log(response.error);
                            }
        				}
        			}
        		}
        	});
        	$('body').on('click','.event-bid-auction',function(event){
        	   var auctionId = $(this).attr('data-auctionId');
        	   client.bidAuction.get(auctionId).bid();
        	   if(client.getHash('needLogin')==1){
                    client.setHash('needLogin',0);
                    $(this).attr({'href':'{$loginUrl}','target':'_blank'});
                    return true;
                }
        	   return false;
        	});
        	
        ");
    }
}